/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <gdbus.h>

#include "connman.h"

static DBusConnection *connection = NULL;

char *connman_dbus_encode_string(const char *value)
{
	GString *str;
	unsigned int i, size;

	if (value == NULL)
		return NULL;

	size = strlen(value);

	str = g_string_new(NULL);
	if (str == NULL)
		return NULL;

	for (i = 0; i < size; i++) {
		const char tmp = value[i];
		if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
						(tmp < 'a' || tmp > 'z'))
			g_string_append_printf(str, "_%02x", tmp);
		else
			str = g_string_append_c(str, tmp);
	}

	return g_string_free(str, FALSE);
}

void connman_dbus_property_append_variant(DBusMessageIter *iter,
    const char *key, int type, const void *val)
{
	DBusMessageIter value;
	const char *signature;

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);

	switch (type) {
	case DBUS_TYPE_BOOLEAN:
		signature = DBUS_TYPE_BOOLEAN_AS_STRING;
		break;
	case DBUS_TYPE_STRING:
		signature = DBUS_TYPE_STRING_AS_STRING;
		break;
	case DBUS_TYPE_BYTE:
		signature = DBUS_TYPE_BYTE_AS_STRING;
		break;
	case DBUS_TYPE_UINT16:
		signature = DBUS_TYPE_UINT16_AS_STRING;
		break;
	case DBUS_TYPE_INT16:
		signature = DBUS_TYPE_INT16_AS_STRING;
		break;
	case DBUS_TYPE_UINT32:
		signature = DBUS_TYPE_UINT32_AS_STRING;
		break;
	case DBUS_TYPE_INT32:
		signature = DBUS_TYPE_INT32_AS_STRING;
		break;
	case DBUS_TYPE_OBJECT_PATH:
		signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
		break;
	default:
		signature = DBUS_TYPE_VARIANT_AS_STRING;
		break;
	}
	dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
	    signature, &value);
	dbus_message_iter_append_basic(&value, type, val);
	dbus_message_iter_close_container(iter, &value);
}

void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key,
		       connman_dbus_append_cb_t function, void *user_data)
{
	DBusMessageIter value, dict;

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);

	dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
			 DBUS_TYPE_ARRAY_AS_STRING
			 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
			 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
			 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &value);

	connman_dbus_dict_open(&value, &dict);
	if (function)
		function(&dict, user_data);
	connman_dbus_dict_close(&value, &dict);

	dbus_message_iter_close_container(iter, &value);
}

void connman_dbus_property_append_string_dict(DBusMessageIter *iter,
		      const char *key, connman_dbus_append_cb_t function,
		      void *user_data)
{
	DBusMessageIter value, dict;

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);

	dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
			 DBUS_TYPE_ARRAY_AS_STRING
			 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
			 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
			 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &value);

	connman_dbus_dict_open(&value, &dict);
	if (function)
		function(&dict, user_data);
	connman_dbus_dict_close(&value, &dict);

	dbus_message_iter_close_container(iter, &value);
}

void connman_dbus_property_append_array(DBusMessageIter *iter,
					const char *key, int type,
					connman_dbus_append_cb_t function,
					void *user_data)
{
	DBusMessageIter value, array;
	const char *variant_sig, *array_sig;

	switch (type) {
	case DBUS_TYPE_BYTE:
		variant_sig = DBUS_TYPE_ARRAY_AS_STRING
			DBUS_TYPE_BYTE_AS_STRING;
		array_sig = DBUS_TYPE_BYTE_AS_STRING;
		break;
	case DBUS_TYPE_STRING:
		variant_sig = DBUS_TYPE_ARRAY_AS_STRING
			DBUS_TYPE_STRING_AS_STRING;
		array_sig = DBUS_TYPE_STRING_AS_STRING;
		break;
	case DBUS_TYPE_OBJECT_PATH:
		variant_sig = DBUS_TYPE_ARRAY_AS_STRING
			DBUS_TYPE_OBJECT_PATH_AS_STRING;
		array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING;
		break;
	default:
		connman_error("%s: type %d not supported", __func__, type);
		return;
	}

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);

	dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
					 variant_sig, &value);

	dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
					 array_sig, &array);
	if (function)
		function(&array, user_data);
	dbus_message_iter_close_container(&value, &array);

	dbus_message_iter_close_container(iter, &value);
}

void connman_dbus_dict_append_variant(DBusMessageIter *dict,
    const char *key, int type, const void *val)
{
	DBusMessageIter entry;

	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
	    NULL, &entry);
	connman_dbus_property_append_variant(&entry, key, type, val);
	dbus_message_iter_close_container(dict, &entry);
}

void connman_dbus_property_append_container(
		DBusMessageIter *iter, const char *key,
		const char *signature, connman_dbus_append_cb_t cb, void *arg)
{
	DBusMessageIter variant;

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);

	dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, signature,
	    &variant);

	cb(&variant, arg);

	dbus_message_iter_close_container(iter, &variant);
}

void connman_dbus_dict_append_variant_array(DBusMessageIter *dict,
    const char *key, int type, connman_dbus_append_cb_t cb, void *arg)
{
	DBusMessageIter entry;

	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
	    NULL, &entry);
	connman_dbus_property_append_array(&entry, key, type, cb, arg);
	dbus_message_iter_close_container(dict, &entry);
}

void connman_dbus_dict_append_variant_container(DBusMessageIter *dict,
    const char *key, const char *signature,
    connman_dbus_append_cb_t cb, void *arg)
{
	DBusMessageIter entry;

	/* TODO(djkurtz): Check each dbus_*() call to detect -ENOMEM */
	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
	    &entry);
	connman_dbus_property_append_container(&entry, key, signature, cb, arg);
	dbus_message_iter_close_container(dict, &entry);
}

connman_bool_t connman_dbus_send_property_changed_variant(const char *path,
    const char *intf, const char *key, int type, const void *val)
{
	DBusMessage *signal;
	DBusMessageIter entry;

	signal = dbus_message_new_signal(path, intf, "PropertyChanged");
	if (signal == NULL) {
		connman_error("%s: path %s intf %s key %s, no signal",
		    __func__, path, intf, key);
		return FALSE;
	}

	dbus_message_iter_init_append(signal, &entry);
	connman_dbus_property_append_variant(&entry, key, type, val);

	g_dbus_send_message(connection, signal);

	return TRUE;
}

connman_bool_t connman_dbus_send_property_changed_dict(const char *path,
    const char *interface, const char *key,
    connman_dbus_append_cb_t function, void *user_data)
{
        DBusMessage *signal;
        DBusMessageIter iter;

        if (path == NULL)
                return FALSE;

        signal = dbus_message_new_signal(path, interface, "PropertyChanged");
        if (signal == NULL)
                return FALSE;

        dbus_message_iter_init_append(signal, &iter);
        connman_dbus_property_append_dict(&iter, key, function, user_data);

        g_dbus_send_message(connection, signal);

        return TRUE;
}

connman_bool_t connman_dbus_send_property_changed_string_dict(const char *path,
    const char *interface, const char *key,
    connman_dbus_append_cb_t function, void *user_data)
{
        DBusMessage *signal;
        DBusMessageIter iter;

        if (path == NULL)
                return FALSE;

        signal = dbus_message_new_signal(path, interface, "PropertyChanged");
        if (signal == NULL)
                return FALSE;

        dbus_message_iter_init_append(signal, &iter);
        connman_dbus_property_append_string_dict(&iter, key, function, user_data);

        g_dbus_send_message(connection, signal);

        return TRUE;
}

connman_bool_t connman_dbus_send_property_changed_array(const char *path,
    const char *intf, const char *key, int type,
    connman_dbus_append_cb_t cb, void *arg)
{
	DBusMessage *signal;
	DBusMessageIter entry;

	signal = dbus_message_new_signal(path, intf, "PropertyChanged");
	if (signal == NULL) {
		connman_error("%s: path %s intf %s key %s, no signal",
		    __func__, path, intf, key);
		return FALSE;
	}

	dbus_message_iter_init_append(signal, &entry);
	connman_dbus_property_append_array(&entry, key, type, cb, arg);

	g_dbus_send_message(connection, signal);

	return TRUE;
}

connman_bool_t connman_dbus_send_property_changed_container(
		const char *path, const char *intf, const char *key,
		const char *signature,
		connman_dbus_append_cb_t cb, void *arg)
{
	DBusMessage *signal;
	DBusMessageIter entry;

	signal = dbus_message_new_signal(path, intf, "PropertyChanged");
	if (signal == NULL)
		return FALSE;

	dbus_message_iter_init_append(signal, &entry);
	connman_dbus_property_append_container(&entry, key, signature, cb, arg);
	g_dbus_send_message(connection, signal);
	return TRUE;
}

DBusConnection *connman_dbus_get_connection(void)
{
	return (connection == NULL ? NULL : dbus_connection_ref(connection));
}

int __connman_dbus_init(DBusConnection *conn)
{
	connection = conn;
	return 0;
}

struct connman_dbus_method_callback {
	connman_dbus_callback_func func;
	DBusMessage *message;
	void *data;
};

struct connman_dbus_method_callback *connman_dbus_callback_new(
		connman_dbus_callback_func func, DBusMessage *msg,
		void *user_data)
{
	struct connman_dbus_method_callback *cb =
			g_new0(struct connman_dbus_method_callback, 1);
	cb->func = func;
	cb->message = dbus_message_ref(msg);
	cb->data = user_data;
	return cb;
}

void connman_dbus_free_callback(struct connman_dbus_method_callback *callback)
{
	if (callback->message != NULL)
		dbus_message_unref(callback->message);
	g_free(callback);
}

void connman_dbus_invoke_callback(struct connman_dbus_method_callback *cb,
				  enum connman_element_error error)
{
	cb->func(error, cb, cb->data);
}

const char *connman_dbus_callback_method_name(
		struct connman_dbus_method_callback *cb)
{
	return cb->message ?  dbus_message_get_member(cb->message) : "";
}

void connman_dbus_callback_send_reply(struct connman_dbus_method_callback *cb,
				      DBusError *error)
{
	DBusMessage *reply;

	if (cb->message == NULL)
		return;
	if (dbus_error_is_set(error))
		reply = dbus_message_new_error(cb->message, error->name, error->message);
	else
		reply = dbus_message_new_method_return(cb->message);
	g_dbus_send_message(connection, reply);
	dbus_message_unref(cb->message);
	cb->message = NULL;
}

void connman_dbus_send_pending_reply(DBusMessage **msg, DBusMessage *reply)
{
	if (reply == NULL)
		reply = dbus_message_new_method_return(*msg);
	g_dbus_send_message(connection, reply);
	dbus_message_unref(*msg);
	*msg = NULL;
}

void __connman_dbus_cleanup(void)
{
	connection = NULL;
}
